#include"../common/dispatcher.hpp"
#include"../server/rpc_router.hpp"


void Add(const Json::Value & prams, Json::Value& result)
{
    int num1 =prams["num1"].asInt();
    int num2 =prams["num2"].asInt();
    result=num1+num2;
    DLOG("服务端本次的Add收到的参数为: %d", num1);
    DLOG("服务端本次的Add收到的参数为: %d", num2);

    DLOG("服务端本次的计算结果为: %d",result.asInt());

}



int main()
{
    //创建dispather以及注册
    auto router = std::make_shared<RPC_project::server::RpcRouter>();
    //实例化工厂对象
    std::unique_ptr<RPC_project::server::SDescribeFactory> desc_factory = std::make_unique<RPC_project::server::SDescribeFactory>();
    desc_factory->setMethodName("Add");
    desc_factory->setParamsDesc("num1" ,RPC_project::server::VType::INTEGRAL);
    desc_factory->setParamsDesc("num2" ,RPC_project::server::VType::INTEGRAL);
    desc_factory->setReturnType(RPC_project::server::VType::INTEGRAL);
    desc_factory->setCallback(Add);
    //用工厂对象生产出对象,并注册进去
    router->registerMethod(desc_factory->build());


    auto rpc_cb =std::bind(&RPC_project::server::RpcRouter::onRpcRequest , router.get() ,std::placeholders::_1 , std::placeholders::_2);
    auto dispather = std::make_shared <RPC_project::Dispather>();

    //dispather收到Rpcrequest后 调用RpcRouter::onRpcRequest进行处理
    dispather->RegisterHandler<RPC_project::RpcRequest>(RPC_project::MType::REQ_RPC, rpc_cb);//消息类型和回调函数
    //dispather->RegisterHandler<RPC_project::TopicRequest>(RPC_project::MType::REQ_TOPIC, onTopicRequest);


    auto message_cb =std::bind(&RPC_project::Dispather::onMessage ,dispather.get() , std::placeholders::_1 ,std::placeholders::_2);
    auto server = RPC_project::ServerFactory::create(8080);
    server->SetMessageCallback(message_cb);
    server->start();

    return 0;
}











// void onRpcRequest(const RPC_project::BaseConnection::ptr & conn, RPC_project::RpcRequest::ptr & msg)
// {
//     std::cout<<"服务器接受到了RPC请求"<<std::endl;
//     //为了打印一下
//     std::string body = msg->Serialize();
//     std::cout<<body<<std::endl;

//     //硬编码了一个response
//     RPC_project::BaseMessage::ptr base_ptr = RPC_project::MessageFactory::Create(RPC_project::MType::RSP_RPC);
//     RPC_project::RpcResponse::ptr rpc_ptr = std::dynamic_pointer_cast<RPC_project::RpcResponse>(base_ptr);
//     rpc_ptr->Setid("666666");
//     rpc_ptr->SetMyType(RPC_project::MType::RSP_RPC);
//     rpc_ptr->SetRcode(RPC_project::RCode::RCODE_OK);
//     rpc_ptr->SetResult(88);
//     conn->Send(rpc_ptr);
// }

// void onTopicRequest(const RPC_project::BaseConnection::ptr & conn, RPC_project::TopicRequest::ptr & msg)
// {
//     std::cout<<"服务器接受到了TOPIC请求"<<std::endl;
//     //为了打印一下
//     std::string body = msg->Serialize();
//     std::cout<<body<<std::endl;

//     //硬编码了一个response
//     RPC_project::BaseMessage::ptr base_ptr = RPC_project::MessageFactory::Create(RPC_project::MType::RSP_TOPIC);
//     RPC_project::TopicResponse::ptr Topic_ptr = std::dynamic_pointer_cast<RPC_project::TopicResponse>(base_ptr);
//     Topic_ptr->Setid("888888");
//     Topic_ptr->SetMyType(RPC_project::MType::RSP_TOPIC);

//     Topic_ptr->SetMyType(RPC_project::MType::RSP_TOPIC);
//     Topic_ptr->SetRcode(RPC_project::RCode::RCODE_OK);

//     conn->Send(Topic_ptr);

// }


// int main()
// {
//     //创建dispather以及注册
    
//     auto dispather = std::make_shared <RPC_project::Dispather>();
//     dispather->RegisterHandler<RPC_project::RpcRequest>(RPC_project::MType::REQ_RPC, onRpcRequest);
//     dispather->RegisterHandler<RPC_project::TopicRequest>(RPC_project::MType::REQ_TOPIC, onTopicRequest);


//     auto message_cb =std::bind(&RPC_project::Dispather::onMessage ,dispather.get() , std::placeholders::_1 ,std::placeholders::_2);

//     auto server = RPC_project::ServerFactory::create(8080);
//     server->SetMessageCallback(message_cb);
//     server->start();

//     return 0;
// }